package com.karileet.nokia.meeting.profile;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.MediaStore.Images;
import android.util.Log;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RemoteViews;

public class MeetingProfileWidgetProvider extends AppWidgetProvider {
	public static String WIDGET_TOGGLE = "Toggle";

	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		final int N = appWidgetIds.length;
		Log.d(MainActivity.APP, "Received widget onUpdate call");
		// Perform this loop procedure for each App Widget that belongs to this
		// provider
		for (int i = 0; i < N; i++) {
			int appWidgetId = appWidgetIds[i];

			// Create an Intent to launch ExampleActivity
			Intent intent = new Intent(context,
					MeetingProfileWidgetProvider.class);
			intent.setAction(WIDGET_TOGGLE);
			PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
					0, intent, 0);
			// Get the layout for the App Widget and attach an on-click listener
			// to the button
			RemoteViews views = new RemoteViews(context.getPackageName(),
					R.layout.meetingprofilewidget);
			// views.setBitmap(R.id.button1, "setImageBitmap", null);
			views.setOnClickPendingIntent(R.id.button1, pendingIntent);
			//set correct widget graphic when widget is added while state is enabled
			if (getState(context)) {
				views.setImageViewResource(R.id.button1, R.drawable.mpbtn3_down);
			} else {
				views.setImageViewResource(R.id.button1, R.drawable.mpbtn3_up);
			}
			// Tell the AppWidgetManager to perform an update on the current app
			// widget
			appWidgetManager.updateAppWidget(appWidgetId, views);
		}
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.d(MainActivity.APP, "Received widget button press");
		super.onReceive(context, intent);
		if (intent.getAction().equals(WIDGET_TOGGLE)) {
			SharedPreferences p = context.getSharedPreferences(
					MainActivity.PREFS_NAME, 0);
			ProfileState state = new ProfileState(context);
			state.setProfileEnabled(p.getBoolean("profile", false));
			state.setVibrateEnabled(p.getBoolean("vibrate", false));
			MeetingProfile mp = new MeetingProfile(context);
			RemoteViews views = new RemoteViews(context.getPackageName(),
					R.layout.meetingprofilewidget);
			if (getState(context)) {
				mp.disable();
				Log.d(MainActivity.APP, "Drawing button-up graphic");
				views.setImageViewResource(R.id.button1, R.drawable.mpbtn3_up);
			} else {
				mp.enable(state);
				Log.d(MainActivity.APP, "Drawing button-down graphic");
				views.setImageViewResource(R.id.button1, R.drawable.mpbtn3_down);
			}
			ComponentName widget = new ComponentName(context,
					MeetingProfileWidgetProvider.class);
			(AppWidgetManager.getInstance(context)).updateAppWidget(widget,
					views);
		}
	}
	private boolean getState(Context context) {
		SharedPreferences p = context.getSharedPreferences(
				MainActivity.PREFS_NAME, 0);
		return p.getBoolean("profile", false);
	}
}
